package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.dto.HomeAdvertiseAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.HomeAdvertiseUpdateDTO;
import cn.tedu.store.commons.pojo.product.entity.HomeAdvertise;
import cn.tedu.store.commons.pojo.product.vo.HomeAdvertiseListItemVO;
import cn.tedu.store.commons.pojo.product.vo.HomeAdvertiseStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.HomeAdvertiseMapper;
import cn.tedu.store.product.service.IHomeAdvertiseService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@DubboService
public class HomeAdvertiseServiceImpl implements IHomeAdvertiseService {
    @Autowired
    private HomeAdvertiseMapper homeAdvertiseMapper;

    @Override
    public void addNew(HomeAdvertiseAddNewDTO homeAdvertiseAddNewDTO) {
        int count = homeAdvertiseMapper.countByName(homeAdvertiseAddNewDTO.getName());
        if(count != 0){
            String message = "添加轮播图失败,名称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        count = homeAdvertiseMapper.countByFileName(homeAdvertiseAddNewDTO.getPic());
        if(count != 0){
            String message = "添加轮播图失败,图片已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        HomeAdvertise homeAdvertise = new HomeAdvertise();
        BeanUtils.copyProperties(homeAdvertiseAddNewDTO,homeAdvertise);
        int rows = homeAdvertiseMapper.insert(homeAdvertise);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id) {
        HomeAdvertiseStandardVO result = homeAdvertiseMapper.getStandardById(id);
        if(result == null){
            String message = "删除轮播图失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = homeAdvertiseMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void setUp(Long id) {
        updateStatusById(id, 1);
    }

    @Override
    public void setDown(Long id) {
        updateStatusById(id, 0);
    }

    @Override
    public void updateById(Long id, HomeAdvertiseUpdateDTO homeAdvertiseUpdateDTO) {
        HomeAdvertiseStandardVO result = homeAdvertiseMapper.getStandardById(id);
        if(result == null){
            String message = "修改轮播图失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        HomeAdvertise homeAdvertise = new HomeAdvertise();
        BeanUtils.copyProperties(homeAdvertiseUpdateDTO,homeAdvertise);
        homeAdvertise.setId(id);
        int rows = homeAdvertiseMapper.updateById(homeAdvertise);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public HomeAdvertiseStandardVO getStandardById(Long id) {
        HomeAdvertiseStandardVO result = homeAdvertiseMapper.getStandardById(id);
        if(result == null){
            String message = "修改轮播图失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public List<HomeAdvertiseListItemVO> list() {
        return homeAdvertiseMapper.list();
    }


    private void updateStatusById(Long id, Integer value){
        String[] tips = {"下线","上线"};
        HomeAdvertiseStandardVO result = homeAdvertiseMapper.getStandardById(id);
        if(result == null){
            String message = tips[value]+"轮播图失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if(result.getStatus() == value){
            String message = "设置"+tips[value]+"轮播图失败,该轮播博图已经是"+tips[value]+"状态!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        HomeAdvertise homeAdvertise = new HomeAdvertise();
        homeAdvertise.setId(id);
        homeAdvertise.setStatus(value);
        int rows = homeAdvertiseMapper.updateById(homeAdvertise);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }
}
